﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Modules.MENU.Components;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI.HtmlControls;

namespace ASPNETVN.PORTAL.Modules.MENU.Controls
{
    public partial class BulletListArcodionMenu : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //JQuery: jquery-1.3.2.js
            string scriptUrl = ResolveClientUrl("~/Scripts/JQuery/jquery-1.3.2.js");
            string scriptName = "jquery";
            Type scriptType = this.GetType();
            ClientScriptManager csm = Page.ClientScript;
            if (!csm.IsClientScriptIncludeRegistered(scriptType, scriptName))
            {
                csm.RegisterClientScriptInclude(scriptType, scriptName, scriptUrl);
            }

            scriptUrl = ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Scripts/ddaccordion.js");
            scriptName = "SlideVerticalMenuScript";
            if (!csm.IsClientScriptIncludeRegistered(scriptType, scriptName))
            {
                csm.RegisterClientScriptInclude(scriptType, scriptName, scriptUrl);
            }


            // Left Pane.
            LiteralControl literal = new LiteralControl("");
            literal.EnableViewState = true;
            LeftPane.EnableViewState = true;

            literal.Text += "<div class='arrowlistmenu'>";

            IList<Item> allItems = Item.SelectCollectionAll(this.ModuleID);
            IList<Item> topItems = allItems.Where(x => x.ParentID == 0).OrderBy(x => x.SortOrder).ToList();

            foreach (Item topItem in topItems)
            {
                //Check have child menu?
                IList<Item> childItems = allItems.Where(x => x.ParentID == topItem.ID).OrderBy(x => x.SortOrder).ToList();

                if (childItems.Count > 0)
                {
                    literal.Text += string.Format("<h3 class='menuheader expandable'>{1}&nbsp;&nbsp;<a href='#'>{0}</a></h3>", topItem.Title, string.Format("<img src='{0}' border='0' />", ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Images/Bullets/Arrow.gif")));

                    literal.Text += "<ul class='categoryitems'>";
                    foreach (Item childItem in childItems)
                    {
                        literal.Text += string.Format("<li><a href='{0}' target='{2}'>{3}&nbsp;&nbsp;{1}</a></li>", childItem.Url, childItem.Title, childItem.Target, string.Format("<img src='{0}' border='0' />", ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Images/Bullets/Arrow3.gif")));
                    }
                    literal.Text += "</ul>";
                }
                else
                    literal.Text += string.Format("<h3 class='menuheader'>{1}&nbsp;&nbsp;<a href='{2}'>{0}</a></h3>", topItem.Title, string.Format("<img src='{0}' border='0' />", ResolveClientUrl("~/Modules/ASPNETVN.PORTAL.Modules.MENU/Images/Bullets/Arrow.gif")), topItem.Url);
            }
            literal.Text += "</div>";
            LeftPane.Controls.Add(literal);


            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = "~/Modules/ASPNETVN.PORTAL.Modules.MENU/StyleSheets/BulletListArcodionMenu.css";
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            Page.Header.Controls.Add(cssLink);
        }

    }
}